package csdn528;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;

public class ReadFileConut {
    public static void main(String[] args) {
        try {
            RandomAccessFile file = new RandomAccessFile("E:/Java项目文件夹/JavaG/MultitHreading/src/csdn528/CountFile.txt", "r");
            HashMap<String,Integer> contF = new HashMap<String,Integer>();
            String str;
            while ((str = file.readLine()) != null) {
                String []words=str.split(",");//分割字符串
                int i=1;
                for(String word:words){//遍历字符串数组
                    word=word.toLowerCase();//将单词转换成小写
                    //判断集合中是否已存有不区分大小写的相同字符串，同时判断字符串是否是空串
                    if(!contF.containsKey(word)&&!word.equals(""))
                    {//将字符串存储到集合中
                        System.out.println("出现的单词有："+word);
                        contF.put(word,i);
                    }
                    else {
                        i++;
                        contF.put(word,i);
                    }
                }
            }
            System.out.println("此句子共出现"+contF.size()+"个单词。");
            System.out.println("每个单词的个数为："+contF);
            //将读取到的数据输出到控制台
            file.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }}